#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 5e4 + 10;

int n;
struct node
{
	int x, y, idx;
}a[N];

priority_queue<PII, vector<PII>, greater<PII>> heap;

bool cmp(node& n1, node& n2)
{
	return n1.x < n2.x;
}

int ans[N];

int main()
{
	cin >> n;
	for(int i = 1;i <= n;i ++)
	{
		a[i].idx = i;
		cin >> a[i].x >> a[i].y;
	}
	sort(a + 1, a + 1 + n, cmp);
	int cnt = 0;
	for(int i = 1;i <= n;i ++)
	{
		int b = a[i].x, e = a[i].y;
		if(heap.empty() || b <= heap.top().first)
		{
			cnt ++;
			ans[a[i].idx] = cnt;
			heap.push({e, cnt});
		}
		else
		{
			auto [u, v] = heap.top(); heap.pop();
			ans[a[i].idx] = v;
			heap.push({e, v});
		}
	}
	cout << cnt << endl;
	for(int i = 1;i <= n;i ++) cout << ans[i] << endl;
	return 0;
}